package com.twu.biblioteca;

import java.io.*;

public class Mainclass
{
    static Library library;
    public static String username,password;
    static int getChoice()                        throws IOException
   {
       DataInputStream get_input= new DataInputStream(System.in);
       System.out.println("****WELCOME TO BANGALORE PUBLIC LIBRARY****\n"+
                          "1.View Books in the Book\n" +
                          "2.Reserve a Book\n"+
                          "3.View Membership Details\n"+
                          "4.View the list of Movies\n");

       String s1=get_input.readLine();
       return (Integer.parseInt(s1));

   }

    public static boolean loginUser(boolean chk_loggedin)               throws IOException
    {

        DataInputStream get_input= new DataInputStream(System.in);
        if(!chk_loggedin)
        {   library=new Library();
            System.out.println("UNAME:");
            username=get_input.readLine();
            System.out.println("PASSWORD:");
            password=get_input.readLine();
            chk_loggedin=library.Authenticate_user(username,password);
        }
       return chk_loggedin;

    }

    static int getBookid()              throws IOException
    {
        DataInputStream get_input= new DataInputStream(System.in);
        System.out.println("Enter Book ID");
        String s2=get_input.readLine();
        return (Integer.parseInt(s2));

    }

    public static void main(String arg[])  throws IOException
    {
        DataInputStream get_input= new DataInputStream(System.in);
        int choice;
        int option;
        library=new Library();
        Boolean flag=false;
        Boolean chk_loggedin=false;

        do
        {   choice=getChoice();
            switch(choice)
            {
                case 1:
                    library.viewBooks();                            //Fn Call To view books in the library
                    break;
                case 2:
                    chk_loggedin=loginUser(chk_loggedin);
                    if(chk_loggedin)
                    {

                        int bookId=getBookid();
                        flag=library.checkBookAvailabilityAndReserve(bookId);       //Fn call to check if the particular book is available
                        if(flag)
                        {
                        System.out.println("Book reserved");
                        }
                        else
                        {
                        System.out.println("Sorry book not available");
                        }
                    }
                    else
                    {
                        System.out.println("Sorry You are not a Member...Please Register");
                    }

                    break;
                case 3:
                    if(chk_loggedin)
                    {
                        library.viewUserDetails(username);                      //Fn call to view the membership details
                    }
                    else
                    {
                        System.out.println("Please talk to Librarian...Thank You");
                    }

                    break;

                case 4:
                    library.viewMovies();
                    break;
                default:
                       System.out.println("Enter valid option");
            }

            System.out.println("Enter 1 to continue");
            String s3=get_input.readLine();
            option=Integer.parseInt(s3);
        }while(option==1);



    }
}

